package com.NeuglsWorkStudio.UI.FormElement.Component
{
	import com.NeuglsWorkStudio.Colors.HSL;
	import com.NeuglsWorkStudio.Colors.RGB;
	import com.NeuglsWorkStudio.UI.Buttons.TTipButton;
	import com.NeuglsWorkStudio.UI.FormElement.Config.TConfig;
	import com.NeuglsWorkStudio.UI.FormElement.Config.TListItemConfig;
	import com.NeuglsWorkStudio.Utility.Utility;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Rectangle;
	
	public class TListItem extends THoverItem
	{
		[Embed(source="images/MessageBox/closeBtn.png")]
		private var CloseBtnImg:Class;
		
		[Embed(source="images/MessageBox/aui_close.hover.png")]
		private var CloseBtnHoverImg:Class;
		
		public function TListItem(AConfig:TConfig)
		{
			super(AConfig);
		}
		
		public var ID:int=0;
		
		protected const cNormal:int=1;
		protected const cOver:int=2;
		protected const cDown:int=3;
		
		
		public function ListItemConfig():TListItemConfig{
			return _FConfig as TListItemConfig;
		}
		
		override protected function Init():void{
			HoverItemConfig().EnableChildMouse=true;
			super.Init();
		}
		
		/**
		 * 根据S获取BitmapData对象
		 * @param S 你要获得BitmapData的类型，可取值为cNormal,cOver,cDown
		 * */
		protected function GetBitmapData(S:int):BitmapData{
			var BorderSize:Number=1;
			var W:Number=this.HoverItemConfig().Width;
			var H:Number=this.HoverItemConfig().Height;
			var Color:int;
			var Alpha:Number;
			var UpBorderColor:int;
			var DownBorderColor:int;
			switch(S){
				case cNormal:
					Color=this.HoverItemConfig().NormalBackColor;
					Alpha=this.HoverItemConfig().NormalBackAlpha;
					//UpBorderColor=this.HoverItemConfig().NormalBorderColor;
					UpBorderColor=HSL.AddLightToColor(this.HoverItemConfig().NormalBorderColor,-0.2);
					BorderSize=this.HoverItemConfig().NormalBorderSize;
					DownBorderColor=HSL.AddLightToColor(this.HoverItemConfig().NormalBorderColor,0.2);
					break;
				case cOver:
					Color=this.HoverItemConfig().HoverBackColor;
					Alpha=this.HoverItemConfig().HoverBackAlpha;
					UpBorderColor=HSL.AddLightToColor(this.HoverItemConfig().HoverBorderColor,-0.2);
					BorderSize=this.HoverItemConfig().HoverBorderSize;
					DownBorderColor=HSL.AddLightToColor(this.HoverItemConfig().HoverBorderColor,0.2);
					break;
				case cDown:
					Color=this.HoverItemConfig().DownBackColor;
					Alpha=this.HoverItemConfig().DownBackAlpha;
					UpBorderColor=HSL.AddLightToColor(this.HoverItemConfig().DownBorderColor,-0.2);
					BorderSize=this.HoverItemConfig().DownBorderSize;
					DownBorderColor=HSL.AddLightToColor(this.HoverItemConfig().DownBorderColor,0.2);
					break;
			}
			
			var R:BitmapData=new BitmapData(W,H,true);
			var LastY:Number=0;
			
			//Draw the up border
			R.fillRect(
				new Rectangle(0,LastY,W,BorderSize),
				Utility.MakeColor(UpBorderColor,0xFF)
			);
			LastY+=BorderSize;
			
			//Draw the body
			R.fillRect(
				new Rectangle(0,LastY,W,H-LastY-BorderSize),
				Utility.MakeColor(Color,Alpha*0xFF)
			);
			LastY+=H-LastY-BorderSize;
			
			//Draw down border
			R.fillRect(
				new Rectangle(0,LastY,W,BorderSize),
				Utility.MakeColor(DownBorderColor,0xFF)
			);
			return R;
		}
		
		protected function ToDraw(S:int):void{
			var NB:BitmapData=GetBitmapData(S);
			with(this._FBack.graphics){
				clear();
				beginBitmapFill(NB);
				drawRect(0,0,this.HoverItemConfig().Width,
					this.HoverItemConfig().Height
				);
				endFill();
			}
			//NB.dispose();
			NB=null;
		}
		
		
		private var _FDeleteOptionBtn:TTipButton;
		public static var OptionBtnTop:Number=10;
		
		private function DisplayOptionBtn(Display:Boolean):void{
			if(!ListItemConfig().OptionalButtons) return;
			var O:Object=ListItemConfig().OptionalButtons;
			if(O.Delete&&Display){
				if(!_FDeleteOptionBtn){
					_FDeleteOptionBtn=new TTipButton('',false);
					_FDeleteOptionBtn.Image=new CloseBtnImg() as Bitmap;
					_FDeleteOptionBtn.MouseOverImg=new CloseBtnHoverImg() as Bitmap;
					_FDeleteOptionBtn.OnClick=function():void{
						O.OnDelete(ID,ExtraData);
					}
					_FDeleteOptionBtn.TipBackgroundColor=this.ListItemConfig().BaseColor;
					_FDeleteOptionBtn.AutoAdjustColor=true;
					_FDeleteOptionBtn.Tip='Click to remove this item';
					this.addChild(_FDeleteOptionBtn);
					_FDeleteOptionBtn.x=this.Width-_FDeleteOptionBtn.width-5;
					_FDeleteOptionBtn.y=OptionBtnTop;
					_FDeleteOptionBtn.ToStopPropagation=true;
				}
				this.setChildIndex(_FDeleteOptionBtn,this.numChildren-1);
				_FDeleteOptionBtn.visible=Display;
			}else{
				if(_FDeleteOptionBtn)
					_FDeleteOptionBtn.visible=Display;
			}
		}
		
		
		override protected function DrawNormal():void{
			ToDraw(cNormal);
			DisplayOptionBtn(false);
		}
		
		override protected function DrawMouseOver():void{
			ToDraw(cOver);
			DisplayOptionBtn(true);
		}
		
		override protected function DrawMouseDown():void{
			ToDraw(cDown);
		}
		
		override protected function DrawMouseUp():void{
			ToDraw(cOver);
		}
		
		/**说明：
		 * 在ListItem的OnClick函数里，我们会调用ListItemConfig的OnClick函数，同时传递ID与ExtraData
		 * 对象*/
		override protected function OnClick():void{
			if(this.HoverItemConfig().OnClick!=null){
				HoverItemConfig().OnClick(this.ID,HoverItemConfig().ExtraData);
			}
		}
	}
}